package com.zzyl;

import org.apache.qpid.proton.codec.security.SaslOutcomeType;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class redis {
    @Autowired
    private RedisTemplate<String,String>redisTemplate;

    @Test
    public void test(){
        System.out.println(redisTemplate);
    }

    @Test
    public void testString(){
        //普通方法
        redisTemplate.opsForValue().set("name","张三");
        System.out.println(redisTemplate.opsForValue().get("name"));
        //设置过期事件
        redisTemplate.opsForValue().set("token","1111",1, TimeUnit.MINUTES);
        System.out.println(redisTemplate.opsForValue().get("token"));
        //setnx
        redisTemplate.opsForValue().setIfAbsent("age","20",1,TimeUnit.MINUTES);
    }
    @Test
    public void testHash(){
    redisTemplate.opsForHash().put("name","张","张友仁");
    redisTemplate.opsForHash().put("name","李","李友仁");
    System.out.println(redisTemplate.opsForHash().get("name","张"));
    redisTemplate.opsForHash().keys("name");
    List<Object> name = redisTemplate.opsForHash().values("name");
    System.out.println(name);
    redisTemplate.opsForHash().delete("name","李");

    redisTemplate.opsForHash().put("hubei","city","wuhan");
    redisTemplate.opsForHash().put("hubei","city","xiangyang");
    redisTemplate.opsForHash().keys("hubei");

    }

    @Test
    public void testList(){
        //插入多个值[a,b,c]
        redisTemplate.opsForList().leftPushAll("name","后入","嵌入","观音坐莲");
        //在列表左边插入一个值[d,a,b,c]
        System.out.println(redisTemplate.opsForList().leftPush("name", "大象"));
        //获取列表中的数据
        System.out.println(redisTemplate.opsForList().range("name", 0, -1));
        //弹出左边第一个
        System.out.println(redisTemplate.opsForList().leftPop("name"));
        //获取列表的长度
        Long name = redisTemplate.opsForList().size("name");
    }

    @Test
    public void testSet(){
        redisTemplate.opsForSet().add("username","忍爱之剑","对魔忍","忍法帖");
        redisTemplate.opsForSet().add("username1.0","a","b");
        //获取集合中的所有数据
        Set<String> username = redisTemplate.opsForSet().members("username");
        username.forEach(System.out::println);
        //获取集合的大小
        Long username1 = redisTemplate.opsForSet().size("username");
        System.out.println(username1);
        //交集
        Set<String> username2 = redisTemplate.opsForSet().intersect("username", "username1.0");
        System.out.println(username2);
        //并
        Set<String> username3 = redisTemplate.opsForSet().union("username", "username1.0");
        System.out.println(username3);
    }
    @Test
    public void testZset(){
        redisTemplate.opsForZSet().add("taoyuan","刘备",10);
        redisTemplate.opsForZSet().add("taoyuan","关羽",10);
        redisTemplate.opsForZSet().add("taoyuan","张飞",10);

        Set<String> taoyuan = redisTemplate.opsForZSet().range("taoyuan", 0, -1);
        System.out.println(taoyuan);

        redisTemplate.opsForZSet().incrementScore("taoyuan", "刘备", 10);

        redisTemplate.opsForZSet().remove("taoyuan","张飞","刘备");
    }

    @Test
    public void testCommon(){
        Set<String> keys = redisTemplate.keys("*");
        System.out.println(keys);

        DataType taoyuan = redisTemplate.type("taoyuan");
        System.out.println(taoyuan);

        Boolean taoyuan1 = redisTemplate.hasKey("taoyuan");
        System.out.println(taoyuan1);

        redisTemplate.delete("taoyuan");

    }


}
